// 通用的错误处理
const ErrorHandler = {
    // 统一的错误处理函数
    handleError: function(error, defaultMessage = '操作失败') {
        let errorMessage = defaultMessage;
        
        if (error.responseJSON && error.responseJSON.message) {
            errorMessage = error.responseJSON.message;
        } else if (error.responseText) {
            try {
                const response = JSON.parse(error.responseText);
                errorMessage = response.message || error.responseText;
            } catch (e) {
                errorMessage = error.responseText;
            }
        } else if (error.message) {
            errorMessage = error.message;
        }
        
        MessageUI.showMessage(errorMessage, 'error');
    }
};

// 消息提示UI
const MessageUI = {
    showMessage: function(message, type = 'info') {
        // 移除旧的提示
        $('.game-alert').remove();
        
        // 根据消息类型设置样式
        let alertClass = 'game-alert';
        switch(type) {
            case 'error':
                alertClass += ' game-alert-error';
                break;
            case 'success':
                alertClass += ' game-alert-success';
                break;
            case 'warning':
                alertClass += ' game-alert-warning';
                break;
            default:
                alertClass += ' game-alert-info';
        }
        
        // 创建新的提示
        const alert = $(`
            <div class="${alertClass}">
                <div class="game-alert-icon">
                    ${this.getAlertIcon(type)}
                </div>
                <div class="game-alert-content">
                    ${message}
                </div>
            </div>
        `);
        
        $('body').append(alert);
        
        // 播放提示音效
        AudioManager.playSound(type);
        
        // 3秒后自动移除
        setTimeout(() => {
            alert.addClass('game-alert-fade-out');
            setTimeout(() => alert.remove(), 300);
        }, 3000);
    },
    
    getAlertIcon: function(type) {
        switch(type) {
            case 'error':
                return '<i class="fas fa-times-circle"></i>';
            case 'success':
                return '<i class="fas fa-check-circle"></i>';
            case 'warning':
                return '<i class="fas fa-exclamation-circle"></i>';
            default:
                return '<i class="fas fa-info-circle"></i>';
        }
    }
};

// 音频管理器
const AudioManager = {
    sounds: {
        select: new Audio('/sounds/card-select.mp3'),
        deselect: new Audio('/sounds/card-deselect.mp3'),
        play: new Audio('/sounds/card-play.mp3'),
        error: new Audio('/sounds/error.mp3'),
        success: new Audio('/sounds/success.mp3'),
        warning: new Audio('/sounds/warning.mp3'),
        info: new Audio('/sounds/info.mp3'),
        bid: new Audio('/sounds/bid.mp3'),
        pass: new Audio('/sounds/pass.mp3'),
        gameStart: new Audio('/sounds/game-start.mp3'),
        gameOver: new Audio('/sounds/game-over.mp3'),
        bomb: new Audio('/sounds/bomb.mp3'),
        rocket: new Audio('/sounds/rocket.mp3'),
        background: new Audio('/sounds/background.mp3')
    },
    
    init: function() {
        // 设置背景音乐循环播放
        this.sounds.background.loop = true;
        // 设置音量
        this.sounds.background.volume = 0.3;
        
        // 预加载所有音效
        Object.values(this.sounds).forEach(sound => {
            sound.load();
        });
    },
    
    playSound: function(type) {
        const sound = this.sounds[type];
        if (sound) {
            sound.currentTime = 0;
            sound.play().catch(() => {}); // 忽略可能的播放错误
        }
    },
    
    playBackgroundMusic: function() {
        this.sounds.background.play().catch(() => {});
    },
    
    stopBackgroundMusic: function() {
        this.sounds.background.pause();
        this.sounds.background.currentTime = 0;
    }
};

// 工具函数
const Utils = {
    formatTime: function(seconds) {
        const minutes = Math.floor(seconds / 60);
        const remainingSeconds = seconds % 60;
        return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`;
    },
    
    debounce: function(func, wait) {
        let timeout;
        return function executedFunction(...args) {
            const later = () => {
                clearTimeout(timeout);
                func(...args);
            };
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
        };
    },
    
    throttle: function(func, limit) {
        let inThrottle;
        return function(...args) {
            if (!inThrottle) {
                func.apply(this, args);
                inThrottle = true;
                setTimeout(() => inThrottle = false, limit);
            }
        };
    }
}; 